package com.UnionPayCenter.account.service.impl;

import com.UnionPayCenter.account.annotation.PayService;
import com.UnionPayCenter.account.entity.ChargeBill;
import com.UnionPayCenter.account.entity.dto.ChargeDTO;
import com.UnionPayCenter.account.entity.dto.PayDTO;
import com.UnionPayCenter.account.enums.AccountErrorCodeEnum;
import com.UnionPayCenter.account.enums.PayChannelEnum;
import com.UnionPayCenter.account.manager.ChargeBillManager;
import com.UnionPayCenter.entity.ShopInfoEntity;
import com.UnionPayCenter.entity.dto.ChargeResponse;
import com.UnionPayCenter.util.IdGen;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 银联支付
 */
@PayService(value = "unionPayService", channel = PayChannelEnum.UNION_PAY)
public class UnionPayService implements com.UnionPayCenter.account.service.PayService {

    @Autowired
    private ChargeBillManager chargeBillService;

    @Override
    public void pay(PayDTO payDTO) {
        AccountErrorCodeEnum.ACCOUNT_PAY_CHANNEL_NOT_EXIST.exception();
    }

    @Override
    public ChargeResponse charge(ChargeBill chargeBill, ChargeDTO chargeDTO, ShopInfoEntity shopInfo) {

        String merchantOrderId = generateMerchantOrderId(shopInfo.getMerchantCode());
        chargeBill.setThirdSn(merchantOrderId);
        String chargeSn = chargeBillService.saveChargeBill(chargeBill);

        ChargeResponse unionpayOrderResponse = new ChargeResponse();
        unionpayOrderResponse.setMerOrderId(merchantOrderId);
        unionpayOrderResponse.setSn(chargeSn);
        return unionpayOrderResponse;
    }

    /**
     * 生成商铺订单号
     * 商户需自行生成merOrderId。此时merOrderId需要符合银商规范，以银商分配的4位系统编号作为订单号的前4位，且在商户系统中此订单号保证唯一。总长度需大于6位，小于32位。
     *
     * @param merchantCode 商铺代码
     * @return 订单号
     */
    private String generateMerchantOrderId(String merchantCode) {
        return IdGen.newStringId();
//        return merchantCode.concat(IdGen.newStringId());
    }
}
